/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatFrame.java
 *
 * Created on 2009-3-11, 22:09:20
 */
package xmppjavaclient;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;

/**
 *
 * @author Eli
 */
public class ChatFrame extends javax.swing.JFrame implements MessageListener {

	/** Creates new form ChatFrame */
	public ChatFrame() {
		initComponents();
	}

	public void setChat(Chat c) {
		this.chat = c;

		lbTo.setText(chat.getParticipant());
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                jScrollPane1 = new javax.swing.JScrollPane();
                txtMsg = new javax.swing.JTextArea();
                lbTo = new javax.swing.JLabel();
                jScrollPane2 = new javax.swing.JScrollPane();
                txtInput = new javax.swing.JTextArea();
                btSend = new javax.swing.JButton();

                jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                jScrollPane1.setAutoscrolls(true);

                txtMsg.setColumns(20);
                txtMsg.setRows(5);
                jScrollPane1.setViewportView(txtMsg);

                lbTo.setText("jLabel1");

                txtInput.setColumns(20);
                txtInput.setRows(5);
                jScrollPane2.setViewportView(txtInput);

                btSend.setText("Send");
                btSend.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                btSendActionPerformed(evt);
                        }
                });

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btSend, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE))
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
                                        .addComponent(lbTo))
                                .addContainerGap())
                );
                layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(lbTo)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(btSend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );

                pack();
        }// </editor-fold>//GEN-END:initComponents

	private void btSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSendActionPerformed
		try {
			chat.sendMessage(txtInput.getText());
			txtMsg.append("Me:" + txtInput.getText() + "\n");
			txtInput.setText("");
		} catch (XMPPException ex) {
			Logger.getLogger(ChatFrame.class.getName()).log(Level.SEVERE, null, ex);
		}
	}//GEN-LAST:event_btSendActionPerformed
	public void processMessage(Chat arg0, Message message) {
		txtMsg.append("Friend:" + message.getBody() + "\n");
	}

        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton btSend;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JLabel lbTo;
        private javax.swing.JTextArea txtInput;
        private javax.swing.JTextArea txtMsg;
        // End of variables declaration//GEN-END:variables
	private Chat chat = null;


}
